﻿Imports Microsoft.Reporting.WebForms

Partial Class UCMain_print
    Inherits System.Web.UI.Page


    Dim dt As New Data.DataTable
    Dim ds As New Data.DataSet
    Protected Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
        loadData()
    End Sub

    Sub loadData()
        Dim sql As String = ""
        Dim reportDataSource = New ReportDataSource()
        Dim dt As New Data.DataTable
        If ddlForm.SelectedValue = "2" Then

            sql += " SELECT r.ID_REQUEST_FORM,r.DUR_WORK_FORM,r.DUR_WORK_TO "
            sql += " ,Switch(r.WORK_LOCA_W='1','West',r.WORK_LOCA_E='1','East') "
            sql += " +' ' +Switch(r.WORK_LOCA_E='1','East') "
            sql += " +' '+Switch(r.WORK_LOCA_SPECIFY='1','Specify by Statuion',r.WORK_LOCA_SPECIFY='2','Specify by Signal') "
            sql += " +' ' +Switch(r.WORK_LOCA_SPECIFY='1','from '+(select station+' '+ area from station where id=r.WORK_LOCA_FROM) +' to ' + (select station+' '+ area from station where id=r.WORK_LOCA_To )"
            sql += " ,WORK_LOCA_SPECIFY='2','from ' + (select signal from [signal] where id = r.WORK_LOCA_FROM) +' to ' + (select signal from [signal] where id = r.WORK_LOCA_TO)) "
            sql += " +' '+Switch(r.WORK_LOCA_W='1' and WORK_LOCA_E='1' and WORK_LOCA_SPECIFY='2'"
            sql += " ,'from West '+(select station+' '+ area from station where id=r.WORK_LOCA_FROM) +' to ' + (select station+' '+ area from station where id=r.WORK_LOCA_TO)"
            sql += " + ' - from East ' + (select station+' '+ area from station where id=r.WORK_LOCA_FROM_S) +' to ' + (select station+' '+ area from station where id=r.WORK_LOCA_TO_S)"
            sql += "  ) as WE"
            sql += " ,WORK_DESC_FROM +' - '+WORK_DESC_TO as desc_work"
            sql += " ,Switch(POWER_REQ='0','Yes') "
            sql += " +' '+Switch(POWER_REQ='0',E1+' '+E2+' '+E3+' '+E4+' '+E5+' '+E6+' '+W1+' '+W2+' '+W3+' '+W4+' '+W5+' '+W6) as energ"
            sql += " ,Switch(TRAIN_REQ='1','Yes',TRAIN_REQ='0','No') "
            sql += " +' '+Switch(VIHECLES_REQ='1','Yes',VIHECLES_REQ='0','No') as vihecle"
            'sql += " ,u.[section] as sc "
            sql += " ,r.STATUS_APP as st"
            sql += " ,r.TP_NO ,r.DATE_REQ "
            sql += " ,(select FIRSTNAME+' '+ LASTNAME as workName from [user_table] where ID_USER=r.WORK_ID_PICOP) as workName"
            sql += " ,(select TEL from [user_table] where ID_USER=r.WORK_ID_PICOP) as workTEL"
            sql += " ,(select [SECTION] from [user_table] where ID_USER=r.WORK_ID_PICOP) as workSECTION"
            sql += " ,(select ID_PICOP from [user_table] where ID_USER=r.WORK_ID_PICOP) as workID_PICOP"
            sql += " ,'" & Session("fname") & "' as issueby "
            sql += " ,date() as issuedate "
            sql += " from Request_table r where 1=1 "
            If Session("section") <> "MMC" Then

                sql += " and u.[section]='" & Session("section") & "'"
            End If


            If txtDateStart.Text <> "" And txtDateEnd.Text <> "" Then
                sql += " and ( format(cdate(r.DUR_WORK_FORM),'yyyyMMdd') >= '" & Utility.clscheckDatetime.ConvertDateTimeDBEngSearch(txtDateStart.SelectedDate) & "' and format(cdate(r.DUR_WORK_TO),'yyyyMMdd') <= '" & Utility.clscheckDatetime.ConvertDateTimeDBEngSearch(txtDateEnd.SelectedDate) & "')"
            End If


            dt = clsCommand.SQL_exec(sql)

            If dt.Rows.Count > 0 Then

                ReportViewer1.ProcessingMode = ProcessingMode.Local
                ReportViewer1.LocalReport.ReportPath = "Report\RTrack_Possession.rdlc"
                ReportViewer1.LocalReport.DataSources.Clear()

                dt.TableName = "dtTrackPossession"
                ds.Tables.Add(dt.Copy)
                reportDataSource.Name = "dsPossession"
                reportDataSource.Value = ds.Tables(0)

                ReportViewer1.LocalReport.DataSources.Add(reportDataSource)
                ReportViewer1.DataBind()
            End If
        Else
            sql += " SELECT r.ID_REQUEST_FORM,r.DUR_WORK_FORM,r.DUR_WORK_TO "
            sql += " ,Switch(r.WORK_LOCA_W='1','/',r.WORK_LOCA_W='0','X') as work_localW"
            sql += " ,Switch(r.WORK_LOCA_E='1','/',r.WORK_LOCA_E='0','X')  as work_localE"
            sql += " ,Switch(r.WORK_LOCA_SPECIFY='1','Specify by Statuion',r.WORK_LOCA_SPECIFY='2','Specify by Signal') as SPECIFY"
            sql += " ,Switch(r.WORK_LOCA_SPECIFY='1','from '+(select station+' '+ area from station where id=r.WORK_LOCA_FROM) +' to ' + (select station+' '+ area from station where id=r.WORK_LOCA_To )"
            sql += " ,WORK_LOCA_SPECIFY='2','from ' + (select signal from [signal] where id = r.WORK_LOCA_FROM) +' to ' + (select signal from [signal] where id = r.WORK_LOCA_TO)) as st_si"
            sql += " ,Switch(r.WORK_LOCA_W='1' and WORK_LOCA_E='1' and WORK_LOCA_SPECIFY='2'"
            sql += " ,'from West '+(select station+' '+ area from station where id=r.WORK_LOCA_FROM) +' to ' + (select station+' '+ area from station where id=r.WORK_LOCA_TO)"
            sql += " + ' - from East ' + (select station+' '+ area from station where id=r.WORK_LOCA_FROM_S) +' to ' + (select station+' '+ area from station where id=r.WORK_LOCA_TO_S)"
            sql += "  ) as WE"
            sql += " ,WORK_DESC_FROM +' - '+WORK_DESC_TO as desc_work"
            sql += " ,Switch(POWER_REQ='0','/') as energ_req"
            sql += " ,Switch(POWER_REQ='1','/') as energ_no"
            sql += " ,Switch(POWER_REQ='0',E1+' '+E2+' '+E3+' '+E4+' '+E5+' '+E6+' '+W1+' '+W2+' '+W3+' '+W4+' '+W5+' '+W6) as energ"
            sql += " ,Switch(TRAIN_REQ='1' or VIHECLES_REQ='1','/ Yes  No',TRAIN_REQ='0' or VIHECLES_REQ='0','Yes  / No') as train"
            sql += " ,Switch(VIHECLES_REQ='1','/',VIHECLES_REQ='0','X') as vihecle"
            sql += " ,r.TP_NO ,r.DATE_REQ "
            sql += " ,(select FIRSTNAME+' '+ LASTNAME as workName from [user_table] where ID_USER=r.WORK_ID_PICOP) as workName"
            sql += " ,(select TEL from [user_table] where ID_USER=r.WORK_ID_PICOP) as workTEL"
            sql += " ,(select [SECTION] from [user_table] where ID_USER=r.WORK_ID_PICOP) as workSECTION"
            sql += " ,(select ID_PICOP from [user_table] where ID_USER=r.WORK_ID_PICOP) as workID_PICOP"
            sql += " ,(select FIRSTNAME+' '+ LASTNAME as workName from [user_table] where ID_USER=r.REQ_ID_PICOP) as reqName"
            sql += " ,(select TEL from [user_table] where ID_USER=r.REQ_ID_PICOP) as reqTEL"
            sql += " ,(select [SECTION] from [user_table] where ID_USER=r.REQ_ID_PICOP) as reqSECTION"
            sql += " ,(select ID_PICOP from [user_table] where ID_USER=r.REQ_ID_PICOP) as reqID_PICOP"

            sql += " ,'" & Session("fname") & "' as issueby "
            sql += " ,date() as issuedate "
            sql += ", r.WORK_APPROVED_BY ,r.COMPANY_SECTION ,r.MOBILE_PHONE "
            sql += " from Request_table r where 1=1 "
            If Session("section") <> "MMC" Then

                sql += " and u.[section]='" & Session("section") & "'"
            End If


            If txtDateStart.Text <> "" And txtDateEnd.Text <> "" Then
                sql += " and ( format(cdate(r.DUR_WORK_FORM),'yyyyMMdd') >= '" & Utility.clscheckDatetime.ConvertDateTimeDBEngSearch(txtDateStart.SelectedDate) & "' and format(cdate(r.DUR_WORK_TO),'yyyyMMdd') <= '" & Utility.clscheckDatetime.ConvertDateTimeDBEngSearch(txtDateEnd.SelectedDate) & "')"
            End If


            dt = clsCommand.SQL_exec(sql)

            If dt.Rows.Count > 0 Then

                ReportViewer1.ProcessingMode = ProcessingMode.Local
                ReportViewer1.LocalReport.ReportPath = "Report\RTP_Request.rdlc"
                ReportViewer1.LocalReport.DataSources.Clear()

                dt.TableName = "dtTPRequest"
                ds.Tables.Add(dt.Copy)
                reportDataSource.Name = "dsTPRequest"
                reportDataSource.Value = ds.Tables(0)

                ReportViewer1.LocalReport.DataSources.Add(reportDataSource)
                ReportViewer1.DataBind()
            End If
        End If

    End Sub

    Protected Sub ddlForm_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlForm.SelectedIndexChanged

    End Sub
End Class
